Skip to content

Implemented a Siamese Network Example #1003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 10, 2022
Merged

Implemented a Siamese Network Example #1003

merged 7 commits into from
May 10, 2022

Conversation

ma7dev
Copy link
Contributor

@ma7dev ma7dev commented May 5, 2022

I have followed mnist/main.py setup and modified it a bit to include the definition of Siamese Network and custom configuration of MNIST set but for the Object Matching task.

This PR was created per #645

@facebook-github-bot
Copy link
Contributor

Hi @sudomaze!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@netlify
Copy link

netlify bot commented May 5, 2022

Deploy Preview for pytorch-examples-preview ready!

Name Link
🔨 Latest commit 332e138
🔍 Latest deploy log https://app.netlify.com/sites/pytorch-examples-preview/deploys/627934fcccb46c2a63fab594
😎 Deploy Preview https://deploy-preview-1003--pytorch-examples-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@msaroufim
Copy link
Member

Can you please setup a test here as well? https://github.com/pytorch/examples/blob/main/run_python_examples.sh

pip install -r requirements.txt
python main.py
# CUDA_VISIBLE_DEVICES=2 python main.py # to specify GPU id to ex. 2
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also reference the paper you're using a baseline for your implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have referenced FaceNet, the closest implementation to the example's implementation. This implementation varies from FaceNet as we use the ResNet-18 model as our feature extractor. In addition, we aren't using TripletLoss as the MNIST dataset is simple, so BCELoss can do the trick.

output1 = self.forward_once(input1)
output2 = self.forward_once(input2)

# concatnate both images' features
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

theres's a few simple typos around, a quick spell checker would help

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing it out! I have fixed it! 😄


def main():
# Training settings
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to Siamese network

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed it. Thanks!

self.dataset = datasets.MNIST(root, train=train, download=download)

# get targets (labels) and data (images)
self.targets = copy.deepcopy(self.dataset.targets)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the goal of the group sets section? Could you add some comments explaining why the deep copies are needed and what this is doing exactly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed unwanted copying of objects and included detailed comments to explain the process.


test_loss /= len(test_loader.dataset)

print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment around how much loss is expected to be if default settings are used

def __len__(self):
return self.data.shape[0]

def __getitem__(self, index):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add more comments in this section

ma7dev added 5 commits May 7, 2022 19:10
- Included `siamese_network` setup to `run_python_examples.sh`
- In `siamese_network/main.py`, included more explanation and detailed comments per @msaroufim's feedback.
`sphinx-serve -d build` doesn't work as `-d` flag doesn't exist. The
correct flag is `-b` per `sphinx-serve` help page. Hence, the edit was
meant to reflect the correct command `sphinx-serve -b build`.
@@ -0,0 +1,7 @@
# Siamese Network Example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK last thing can you please quickly explain what a Siamese network is here, the high-level architecture and what users should expect to input and get as an output for this example. Not a tutorial.

Also, post some proof in a screenshot here on the PR that this thing works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have included an explanation in the class definition. I will update the read me to include the same definition.

Here is a screenshot to prove that the model works using the default setup.

Screenshot from 2022-05-10 21-44-58

@msaroufim msaroufim self-requested a review May 10, 2022 20:31
@msaroufim msaroufim merged commit 41b035f into pytorch:main May 10, 2022
YinZhengxun pushed a commit to YinZhengxun/mt-exercise-02 that referenced this pull request Mar 30, 2025
…k_example

Implemented a Siamese Network Example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants